/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import { message, Popconfirm } from 'antd';
import { CHAIN_COLUMNS } from '@/components/_pages/jdk/index.d';
import request from '@/utils/request';
import ProForm, { ModalForm, ProFormTextArea } from '@ant-design/pro-form';

interface RefuseModalProps {
  api: string;
  status: boolean;
  actionRef: any;
  onComplete?: Function;
  queryData: any;
  inhibitedApi: string;
  inhibitedQueryData: any;
  reamrkApi?: string;
  reamrkQueryData?: any;
  item: CHAIN_COLUMNS;
}

const RefuseModal: React.FC<RefuseModalProps> = ({
  api,
  status,
  actionRef,
  onComplete,
  reamrkApi,
  queryData,
  reamrkQueryData,
  inhibitedApi,
  inhibitedQueryData,
}) => {
  const onReset = () => {
    if (actionRef) {
      actionRef.current?.reload();
    }
    if (onComplete) {
      onComplete();
    }
  };

  const onSubmit = async (values: any) => {
    const queryDatas = reamrkApi ? queryData : { ...queryData, ...values };
    request(api, {
      method: 'POST',
      data: queryDatas,
    }).then(async (res) => {
      const { code, msg } = res;
      if (code == 0) {
        if (reamrkApi) {
          const { code: code_, msg: msg_ } = await request(reamrkApi, {
            method: 'POST',
            data: { ...reamrkQueryData, ...values },
          });
          if (code_ == 0) {
            message.success('禁止登录成功！');
            onReset();
            return true;
          } else {
            message.error(msg_);
            return false;
          }
        } else {
          message.success(msg);
          onReset();
          return true;
        }
      } else {
        message.error(msg);
        return false;
      }
    });
  };
  const handleInhibited = () => {
    request(inhibitedApi, {
      method: 'POST',
      data: inhibitedQueryData,
    }).then((res) => {
      const { code, msg } = res;
      if (code == 0) {
        message.success(msg);
        onReset();
        return true;
      } else {
        message.error(msg);
        return false;
      }
    });
  };
  return (
    <div>
      {status ? (
        <ModalForm
          title='温馨提示'
          trigger={<a style={{ color: 'red' }}>禁止登录</a>}
          width={480}
          modalProps={{
            destroyOnClose: true,
          }}
          onFinish={onSubmit}
        >
          <h3>确定此用户禁止登录？</h3>
          <ProForm.Group>
            <ProFormTextArea
              width='xl'
              name='remark'
              placeholder='填写原因'
              allowClear
              rules={[{ required: true, message: '请填写原因' }]}
              fieldProps={{
                rows: 4,
                maxLength: 20,
              }}
            />
          </ProForm.Group>
        </ModalForm>
      ) : (
        <Popconfirm title={`你确定允许登录吗？`} onConfirm={handleInhibited} key='approved'>
          <a>允许登录</a>
        </Popconfirm>
      )}
    </div>
  );
};

export default RefuseModal;
